package com.ruoyi.sso.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sso.domain.SsoWxToken;
import com.ruoyi.sso.service.ISsoWxTokenService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * tokenController
 * 
 * @author zsj
 * @date 2020-01-07
 */
@Controller
@RequestMapping("/sso/token")
public class SsoWxTokenController extends BaseController
{
    private String prefix = "sso/token";

    @Autowired
    private ISsoWxTokenService ssoWxTokenService;

    @RequiresPermissions("sso:token:view")
    @GetMapping()
    public String token()
    {
        return prefix + "/token";
    }

    /**
     * 查询token列表
     */
    @RequiresPermissions("sso:token:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SsoWxToken ssoWxToken)
    {
        startPage();
        List<SsoWxToken> list = ssoWxTokenService.selectSsoWxTokenList(ssoWxToken);
        return getDataTable(list);
    }

    /**
     * 导出token列表
     */
    @RequiresPermissions("sso:token:export")
    @Log(title = "token", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SsoWxToken ssoWxToken)
    {
        List<SsoWxToken> list = ssoWxTokenService.selectSsoWxTokenList(ssoWxToken);
        ExcelUtil<SsoWxToken> util = new ExcelUtil<SsoWxToken>(SsoWxToken.class);
        return util.exportExcel(list, "token");
    }

    /**
     * 新增token
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存token
     */
    @RequiresPermissions("sso:token:add")
    @Log(title = "token", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SsoWxToken ssoWxToken)
    {
        return toAjax(ssoWxTokenService.insertSsoWxToken(ssoWxToken));
    }

    /**
     * 修改token
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SsoWxToken ssoWxToken = ssoWxTokenService.selectSsoWxTokenById(userId);
        mmap.put("ssoWxToken", ssoWxToken);
        return prefix + "/edit";
    }

    /**
     * 修改保存token
     */
    @RequiresPermissions("sso:token:edit")
    @Log(title = "token", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SsoWxToken ssoWxToken)
    {
        return toAjax(ssoWxTokenService.updateSsoWxToken(ssoWxToken));
    }

    /**
     * 删除token
     */
    @RequiresPermissions("sso:token:remove")
    @Log(title = "token", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ssoWxTokenService.deleteSsoWxTokenByIds(ids));
    }
}
